/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.mpd.musicjukebox.ui.components;

import com.bff.mpd.musicjukebox.lib.ColorProperties;
import java.awt.Component;
import java.util.HashMap;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import org.bff.javampd.objects.MPDSong;

/**
 *
 * @author Bill
 */
public class PlaylistCellRenderer extends DefaultListCellRenderer {

    private HashMap<MPDSong, PlaylistCellPanel> listPanels;
    private MPDSong currentSong;

    public PlaylistCellRenderer() {
        listPanels = new HashMap<MPDSong, PlaylistCellPanel>();
    }

    @Override
    public Component getListCellRendererComponent(JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
       
        if (!(value instanceof MPDSong)) {
            return this;
        }

        MPDSong object = (MPDSong) value;

        PlaylistCellPanel pcp = getListPanels().get(object);

        if (pcp != null) {
            if ((index % 2) == 0) {
                pcp.setEven();
            } else {
                pcp.setOdd();
            }

            if (!isSelected) {
                pcp.setForeground(ColorProperties.LIST_FOREGROUND);
                pcp.setNotSelected();
            } else {
                pcp.setSelected();
            }

            if (getCurrentSong() != null) {
                if (object.equals(getCurrentSong())) {
                    pcp.setCurrentPlaying();
                } else {
                    pcp.setNotPlaying();
                }
            } else {
                pcp.setNotPlaying();
            }
            return (pcp);
        } else {
            pcp = new PlaylistCellPanel(object, list);
            getListPanels().put(object, pcp);
            return pcp;
        }
    }

    /**
     * @return the listPanels
     */
    public HashMap<MPDSong, PlaylistCellPanel> getListPanels() {
        return listPanels;
    }

    /**
     * @return the currentSong
     */
    public MPDSong getCurrentSong() {
        return currentSong;
    }

    /**
     * @param currentSong the currentSong to set
     */
    public void setCurrentSong(MPDSong currentSong) {
        this.currentSong = currentSong;
    }
}
